﻿using System.Collections.Generic;
using UnityEngine;

namespace Game
{
    public class SceneMonsterEntity : SceneBattleEntity
    {
        private PetConfigData _petData;
        private Transform _spriteTransform;
        private SpriteRenderer _spriteRenderer;
        private SceneMonsterBehaviorComponent _behaviorComponent;
        
        public SceneMonsterEntity(int id) : base(id, SceneObjectEntityType.Monster)
        {
        }

        public void LoadMonster(int configId)
        {
            if (_petData != null && _petData.id == configId)
            {
                return;
            }

            _petData = ConfigSystem.Instance.Get<PetConfigEntity>().GetData(configId);
            Load(_petData.url);
        }
        
        public override void SetFlipX(bool flip)
        {
            _flipX = flip;
            if (_isCompleted)
            {
                _spriteRenderer.flipX = flip;  
            }
        }

        public override void SetFlipY(bool flip)
        {
            _flipY = flip;
            if (_isCompleted)
            {
                _spriteRenderer.flipY = flip;
            }
        }
        
        public override void BeHit(SceneObjectEntity attackEntity)
        {
            base.BeHit(attackEntity);
            _behaviorComponent.BeHit(attackEntity);
        }
        
        public override void Die()
        {
            SceneObjectFinderComponent objectFinderComponent = GetComponent<SceneObjectFinderComponent>();
            objectFinderComponent.RemoveMonster(this);
            base.Die();
        }
        
        public override void Update(float deltaTime)
        {
            base.Update(deltaTime);
            if (_behaviorComponent == null)
            {
                return;
            }
            _behaviorComponent.CollidedWith(null);
            SceneObjectFinderComponent finderComponent = GetComponent<SceneObjectFinderComponent>();
            if (finderComponent == null)
            {
                return;
            }
            float monsterX = position.x;
            List<ScenePetEntity> scenePetList = finderComponent.GetScenePetList();
            for (int i = 0; i < scenePetList.Count; ++i)
            {
                ScenePetEntity scenePet = scenePetList[i];
                if (scenePet.GetComponent<SceneObjectBattlePropertiesComponent>()
                    .GetProperty(SceneObjectBattlePropertyKeys.Hp) <= 0)
                {
                    continue;
                }
                float petX = scenePet.position.x;
                if (monsterX < petX && !GetFlipY() || monsterX > petX && GetFlipY())
                {
                    if (scenePet.IsCollided(position, bounds))
                    {
                        _behaviorComponent.CollidedWith(scenePet);
                        break;
                    }
                }
            }
        }
        
        protected override void PostInstantiate()
        {
            base.PostInstantiate();
            _spriteTransform = _contentTransform.Find("Sprite");
            _spriteRenderer = _spriteTransform.GetComponent<SpriteRenderer>();
            SetFlipX(_flipX);
            SetFlipY(_flipY);

            SceneObjectBattlePropertiesComponent propertiesComponent = GetComponent<SceneObjectBattlePropertiesComponent>();
            propertiesComponent.SetProperty(SceneObjectBattlePropertyKeys.MaxHp, 5);
            propertiesComponent.SetProperty(SceneObjectBattlePropertyKeys.Hp, 5);
            
            _behaviorComponent = AddComponent<SceneMonsterBehaviorComponent>();
            _behaviorComponent.Setup(this);
            
            OnPostInstantiateCompleted();
        }
        
        protected override void UpdateSortingLayer(int layerId)
        {
            base.UpdateSortingLayer(layerId);
            _spriteRenderer.sortingLayerID = layerId;
        }

        protected override void UpdateSortingOrder(int order)
        {
            base.UpdateSortingOrder(order);
            _spriteRenderer.sortingOrder = order;
        }
    }
}